/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.EnumerationLiteral;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;


/**
 * 
 */
public class DefaultValueEnumSection extends PropertyViewSection {



	/**
	 * 
	 */
	private CCombo valueCombo;
	
	/**
	 * 
	 */
	private SelectionListener valueComboListener;


	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 * 
	 * @see org.eclipse.ui.views.properties.tabbed.ITabbedPropertySection#createControls(org.eclipse.swt.widgets.Composite,
	 * org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;



		valueCombo = getWidgetFactory().createCCombo(composite, SWT.NONE);  
		data = new FormData();
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
		data.top = new FormAttachment(0, 0);
		valueCombo.setLayoutData(data);

		valueCombo.addSelectionListener(valueComboListener = new SelectionListener () {
			public void widgetSelected(SelectionEvent e) {
				((Property)getElement()).setDefault(valueCombo.getItem(valueCombo.getSelectionIndex()));
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});


		getWidgetFactory().createCLabel(composite, "DefaultValue:"); //$NON-NLS-1$

	}



	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * 
	 */
	@Override
	public void refresh() {

		if(!valueCombo.isDisposed()){
			if(getElement() instanceof Property) {
				valueCombo.removeAll();

				List<EnumerationLiteral> enumLiterals = ((Enumeration)((Property)getElement()).getType()).getOwnedLiterals();
				for(int i=0; i<enumLiterals.size(); i++) {
					valueCombo.add(enumLiterals.get(i).getLabel());

					if((((Property)getElement()).getDefault() != null) &&
							((Property)getElement()).getDefault().equals(enumLiterals.get(i).getLabel())) {
						valueCombo.select(i);
					}
				}


			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(valueCombo!=null && !valueCombo.isDisposed())
			valueCombo.removeSelectionListener(valueComboListener);
	}

}

